package com.bingxue.edu.framework.common.model;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Schema(title = "统一响应返回实体")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Response<T> implements Serializable {

    public static final int SUCCESS = 0;

    @Schema(title = "响应编码")
    private int code;

    @Schema(title = "响应消息")
    private String message;

    @Schema(title = "响应数据")
    private T data;

    /**
     * 静态构造成功响应体
     *
     * @return 响应
     */
    public static  Response<Void> success() {
        return success(null);
    }

    /**
     * 静态构造带数据的成功响应体
     *
     * @param data 响应数据
     * @param <T>  数据类型
     * @return 响应
     */
    public static <T> Response<T> success(T data) {
        return new Response<>(SUCCESS, "操作成功", data);
    }

    /**
     * 静态构造带错误响应体
     *
     * @param code    错误码
     * @param message 错误提示
     * @return 响应
     */
    public static Response<Void> fail(int code, String message) {
        return new Response<>(code, message, null);
    }

}
